package com.ys.zero.web;

import java.io.IOException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ys.zero.bean.Result;
import com.ys.zero.util.EmailUtils;
import com.ys.zero.util.VerifyRound;




@WebServlet("/sendEmial.s")
public class SendEmialservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
	
	protected  void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8"); 
//		String param = request.getQueryString();
//		//测试   System.out.println(param);
//		//使用正则表达式获取到邮箱
//		String  regex = "(\\w+)@(\\w+)\\.(\\w+)";
//		Pattern p = Pattern.compile(regex);
//		Matcher m = p.matcher(param);
//		while(m.find()) {
//			email = m.group(0);
//			System.out.println(m.group(0));
//		}
		String code =  request.getParameter("code");
		ServletContext context = getServletContext(); 
		String verify = (String) context.getAttribute("verify");
		if(code==null) {
			String json = new Gson().toJson(new Result(0,"验证码不能为空"));
			response.getWriter().append(json);
			return;
		}
		if(verify.equals(code)) {
			String json = new Gson().toJson(new Result(1,"验证成功"));
			response.getWriter().append(json);
		}else {
			String json = new Gson().toJson(new Result(0,"验证码错误"));
			response.getWriter().append(json);
		}
		
		
//		EmailUtils eu = new EmailUtils();
//		VerifyRound v  = new VerifyRound();
//		String verify = v.getRound();
//		context.setAttribute("verify",verify);
//		System.out.println("====sendemial:"+verify);
//		String Verification ="验证码：" + verify +"    请在15分钟内完成，请勿把验证码给其他人。";
		
//		if(code != null) {
//			eu.sendMail(eMail, "零号网验证码", Verification);
//		}else {
//			String json = new Gson().toJson(new Result(0,"验证码不能为空"));
//			response.getWriter().append(json);
//		}
		
	}
	/*
	 * public static void main(String [] args) { String param =
	 * "mail=2052314881@qq.com"; String regex = "(\\w+)@(\\w+)\\.(\\w+)"; Pattern p
	 * = Pattern.compile(regex); Matcher m = p.matcher(param); while(m.find()) {
	 * System.out.println(m.group(0)); } }
	 */

}
